/*
	Copyright (C) 2011 Salil Bhagurkar

	This file is part of illusion

	illusion is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation, either version 3 of
	the License, or (at your option) any later version.

	illusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with illusion. 
	If not, see <http://www.gnu.org/licenses/>.
*/

#include <klib/lib.h>
#include <arch/power.h>
#include <klib/string.h>
#include <kernel/vfs.h>
#include <kernel/errors.h>

/*
 * System power interfaces for the user mode
 */

static int sys_pwr_sync_page(struct vfs_node *node, struct vfs_page *page)
{
	if(streq(page->stream, "name")) {
		char *op = page->page_data;
		if(streq(page->stream, "halt")) {
			halt_system();
		} else if(streq(page->stream, "reboot")) {
			reboot_system();
		}
		return 0;
	} else
		return ENOSUP;
}

static struct vfs_sync_provider power_prov = {
		.flags = VFS_SP_NOPAGECACHE,
		.supported_page_streams = "name",
		.name = "sys_pwr_sync",
		.sync_node = null,
		.sync_page = sys_pwr_sync_page,
};

int power_init()
{
	struct vfs_device *power_dev = vfs_create_device("sys_pwr", 1);
	vfs_set_sync_provider(power_dev, &power_prov);
	return 0;
}
